package com.picket.controller.matches;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.picket.domain.Event;
import com.picket.domain.Match;
import com.picket.service.IEventService;
import com.picket.service.IMatchService;
import com.picket.service.IUserPoolService;
import com.picket.service.IUserPredictionService;

@Controller
@RequestMapping(value = "admin/matches/results")
public class MatchResultController {

	private IEventService eventService;

	@Required
	public void setEventService(IEventService eventService) {
		this.eventService = eventService;
	}

	private IMatchService matchService;

	@Required
	public void setMatchService(IMatchService matchService) {
		this.matchService = matchService;
	}

	private IUserPoolService userPoolService;

	@Required
	public void setUserPoolService(IUserPoolService userPoolService) {
		this.userPoolService = userPoolService;
	}

	private IUserPredictionService userPredService;

	@Required
	public void setUserPredictionService(IUserPredictionService userPredService) {
		this.userPredService = userPredService;
	}

	@ModelAttribute("matchResultForm")
	public MatchResultForm createForm() {
		return new MatchResultForm();
	}

	@RequestMapping(value = "event", method = RequestMethod.GET)
	public String getResultForm(
			@ModelAttribute("matchResultForm") MatchResultForm matchResultForm,
			Model model) {
		Collection<Event> events = eventService.getAllEvents();

		model.addAttribute("events", events);
		model.addAttribute("matchResultForm", matchResultForm);
		return "/matches/selectEvent";
	}

	@RequestMapping(value = "match/{id}", method = RequestMethod.GET)
	public String getResultMatchForm(@PathVariable Long id,
			@ModelAttribute("matchResultForm") MatchResultForm matchResultForm,
			Model model) {
		Collection<Match> matches = matchService.getAllMatchesForEvent(id);

		model.addAttribute("matches", matches);
		model.addAttribute("matchResultForm", matchResultForm);
		return "/matches/selectMatch";
	}

	@RequestMapping(value = "match/result/{id}", method = RequestMethod.GET)
	public String getResultMatchResultForm(@PathVariable Long id,
			@ModelAttribute("matchResultForm") MatchResultForm matchResultForm,
			Model model) {
		Match match = matchService.getMatch(id);
		matchResultForm.setEventId(match.getEventId());
		matchResultForm.setMatchId(match.getId());
		model.addAttribute("match", match);

		model.addAttribute("matchResultForm", matchResultForm);
		return "/matches/enterResult";
	}

	@RequestMapping(value = "selectMatch", method = RequestMethod.POST)
	public String selectMatch(
			@ModelAttribute("matchResultForm") MatchResultForm matchResultForm,
			BindingResult result, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		// return null;
		return "redirect:match/" + matchResultForm.getSelected();
	}

	@RequestMapping(value = "match/matchResult", method = RequestMethod.POST)
	public String selectMatchResult(
			@ModelAttribute("matchResultForm") MatchResultForm matchResultForm,
			BindingResult result, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		// return null;
		return "redirect:result/" + matchResultForm.getSelected();
	}

	@RequestMapping(value="match/result/updateResult", method = RequestMethod.POST)
	public String saveMatchResult(@ModelAttribute("matchResultForm") MatchResultForm matchResultForm, BindingResult result,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		long eventId=matchResultForm.getEventId();
		long matchId = matchResultForm.getMatchId();
		String matchResult = matchResultForm.getResult();
		// update match results  and match points... need match id
		// move all to one transaction
		Match match = matchService.updateMatch(matchId, matchResult);
		// update user prediction table with prediction from user...  need match id
		userPredService.updatePredictionsWithPoints(match);
		userPoolService.updatePoints(eventId);
		//userPoolService.updateStandings(eventId);
		// update userpool table with ranking...
		return "redirect:/admin/matches/results/event";
	}

	@RequestMapping(value = "all", method = RequestMethod.GET)
	public String getView(Model model) {
		Collection<Match> matches = matchService.getAllMatches();

		model.addAttribute("matches", matches);
		return "/matches/view";
	}
}